﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CdcSoftware.Pivotal.Applications.Core.Client;
using CdcSoftware.Pivotal.Engine.UI.Forms;
using CdcSoftware.Pivotal.Applications.Core.Common;
using CdcSoftware.Pivotal.Engine.Client.Services.Interfaces;
using System.Data;
using CdcSoftware.Pivotal.Engine;

namespace Pcm.Retail.Client.FormTasks
{
    public partial class RE_RAE_Transaction : FormClientTask
    {        
        /// <summary>
        /// Event gets triggered with the click of Add Item Button.
        /// </summary>
        /// <param name="sender">The control associated with the event triggered</param>
        /// <param name="args">The argument for the event</param>
        /// <history>
        /// #Revision   Date            Author      Description
        /// 6.0.12.0     2013-06-04      Pkini       Initial version for Retail.
        /// </history>
        public virtual void OnButtonClickCallAppleCareAutoEnroll(PivotalControl sender, EventArgs args)
        {
            try
            {
                string message = string.Empty;
                this.FormView.ValidateRequiredFields(out message);

                if (message != string.Empty)
                {
                    PivotalMessageBox.Show(message, System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
                    return;
                }

                Id recordId = (Id)Globals.SystemClient.ExecuteServerTask("RE_Sale", "CallAppleCareCreateOrCancelOrder", new Type[] { typeof(DataTable),typeof(Id) }, new object[] { this.DataSet.Tables[0],this.RecordId });

                if (this.RecordId == null)
                {
                    this.FormView.CloseForm(true);
                    Globals.ShowForm("RE_RAE_Transaction", recordId);
                }
                else
                {
                    this.FormView.LoadRecord();
                }
            }
            catch (Exception exc)
            {
                Globals.HandleException(exc, true);
            }
        }

        public virtual void OnUpdateTransactionType(PivotalControl sender, EventArgs args)
        {
            try
            {
                if (String.Equals(TypeConvert.ToString(this.PrimaryDataRow["RAE_Transaction"]), "CancelAEOrder"))
                {
                    Globals.EnableDisablePrimaryFields(false, new string[] { "Contact_First_Name", "Contact_Last_Name", "Contact_Email",
                    "Date_of_Purchase"});
                    ((PivotalTextBox)this.FormControl.GetControlByDataName("Contact_First_Name")).Required = false;
                    ((PivotalTextBox)this.FormControl.GetControlByDataName("Contact_Last_Name")).Required = false;
                    ((PivotalTextBox)this.FormControl.GetControlByDataName("Contact_Email")).Required = false;
                    ((PivotalTextBox)this.FormControl.GetControlByDataName("Date_of_Purchase")).Required = false;
                }
                else
                {
                    Globals.EnableDisablePrimaryFields(true, new string[] { "Contact_First_Name", "Contact_Last_Name", "Contact_Email",
                    "Date_of_Purchase"});
                    ((PivotalTextBox)this.FormControl.GetControlByDataName("Contact_First_Name")).Required = true;
                    ((PivotalTextBox)this.FormControl.GetControlByDataName("Contact_Last_Name")).Required = true;
                    ((PivotalTextBox)this.FormControl.GetControlByDataName("Contact_Email")).Required = true;
                    ((PivotalTextBox)this.FormControl.GetControlByDataName("Date_of_Purchase")).Required = true;
                }
            }
            catch (Exception ex)
            {
                Globals.HandleException(ex, true);
            }
        }

        public virtual void OnDataSourceUpdatedEmail(PivotalControl sender, EventArgs args)
        {
            try
            {
                PivotalTextBox currentControl = (PivotalTextBox)sender;
                string emailAddress = currentControl.Text;

                if (!String.IsNullOrEmpty(emailAddress))
                {
                    if (!InteractionService.ValidateEmailAddress(emailAddress, true))
                    {
                        this.PrimaryDataRow["Contact_Email"] = DBNull.Value;
                        //System.ComponentModel.CancelEventArgs cancelArgs = args as System.ComponentModel.CancelEventArgs;
                        //cancelArgs.Cancel = true;
                    }
                }

                //if (this.PrimaryDataRow["RE_Contact_Email"] != DBNull.Value)
                //{
                //    bool validEmail = ValidateEmailAddress(TypeConvert.ToString(this.PrimaryDataRow["RE_Contact_Email"]));
                //    if (!validEmail)
                //    {
                //        PivotalMessageBox.Show("Please enter a valid Email.", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                //        return;
                //    }
                //}
            }
            catch (Exception ex)
            {
                Globals.HandleException(ex);
            }
        }

        public virtual void OnDataSourceUpdatedSource(PivotalControl sender, EventArgs args)
        {
            try
            {
                if (this.RecordId == null)
                {
                    if (String.Equals(TypeConvert.ToString(this.PrimaryDataRow["Source_"]), "Town Stores"))
                    {
                        PivotalMessageBox.Show("New Apple Care Transaction cannot be created for Town Stores", System.Windows.Forms.MessageBoxButtons.OK
                            , System.Windows.Forms.MessageBoxIcon.Asterisk);
                        this.PrimaryDataRow["Source_"] = DBNull.Value;
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                Globals.HandleException(ex);
            }
        }


        public virtual void OnDataSourceUpdatedTransactionType(PivotalControl sender, EventArgs args)
        {
            try
            {
                if (String.Equals(TypeConvert.ToString(this.PrimaryDataRow["RAE_Transaction"]), "CancelAEOrder"))
                {
                    Globals.EnableDisablePrimaryFields(false, new string[] { "Contact_First_Name", "Contact_Last_Name", "Contact_Email",
                             "Date_of_Purchase"});
                    ((PivotalTextBox)this.FormControl.GetControlByDataName("Contact_First_Name")).Required = false;
                    ((PivotalTextBox)this.FormControl.GetControlByDataName("Contact_Last_Name")).Required = false;
                    ((PivotalTextBox)this.FormControl.GetControlByDataName("Contact_Email")).Required = false;
                    ((PivotalTextBox)this.FormControl.GetControlByDataName("Date_of_Purchase")).Required = false;
                }
                else
                {
                    Globals.EnableDisablePrimaryFields(true, new string[] { "Contact_First_Name", "Contact_Last_Name", "Contact_Email",
                            "Date_of_Purchase"});
                    ((PivotalTextBox)this.FormControl.GetControlByDataName("Contact_First_Name")).Required = true;
                    ((PivotalTextBox)this.FormControl.GetControlByDataName("Contact_Last_Name")).Required = true;
                    ((PivotalTextBox)this.FormControl.GetControlByDataName("Contact_Email")).Required = true;
                    ((PivotalTextBox)this.FormControl.GetControlByDataName("Date_of_Purchase")).Required = true;
                }
            }
            catch (Exception ex)
            {
                Globals.HandleException(ex);
            }
        }
    }
}
